package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.PageBean;
import com.itheima.dto.ClazzDto;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.service.ClazzService;
import com.itheima.vo.ClazzVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    ClazzMapper clazzMapper;

    /**
     * 分页查询班级
     *
     * @param dto
     * @return
     */
    @Override
    public PageBean pageGet(ClazzDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<ClazzVo> vo = clazzMapper.list(dto);
        PageBean bean = new PageBean(vo.getTotal(), vo.getResult());
        return bean;
    }

    /**
     * 添加班级
     *
     * @param clazz
     */
    @Override
    public void insert(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insert(clazz);
    }

    /**
     * 根据id查询班级
     *
     * @param id
     * @return
     */
    @Override
    public Clazz getById(Integer id) {
       Clazz clazz = clazzMapper.getById(id);
        return clazz;
    }

    /**
     * 修改班级
     *
     * @param clazz
     */
    @Override
    public void updateById(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateById(clazz);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<Clazz> getAll() {
       List <Clazz> clazz=clazzMapper.getAll();
        return clazz;
    }

    /**
     * 删除班级
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        clazzMapper.deleteById(id);
    }
}
